
package com.pricemodule.dao;
import com.pricemodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbPriceDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbPrice tbPrice=null;
    private List<TbPrice> tbPriceList=null;

    public boolean add(TbPrice inTbPrice){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Price(PriceYear,PriceValue,PriceMemo,CreateTime) values(?,?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbPrice.getPriceYear());
        pt.setString(2,inTbPrice.getPriceValue());
        pt.setString(3,inTbPrice.getPriceMemo());
        pt.setString(4,inTbPrice.getCreateTime());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inPriceId){
        return UtilDAO.del("Tb_Price", "PriceId", inPriceId);
    }
    public boolean edit(TbPrice inTbPrice){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Price set  PriceYear=?,PriceValue=?,PriceMemo=? where PriceId=?";
        pt = conn.prepareStatement(sql);
        
      
        pt.setString(1,inTbPrice.getPriceYear());
        pt.setString(2,inTbPrice.getPriceValue());
        pt.setString(3,inTbPrice.getPriceMemo());
        pt.setInt(4,inTbPrice.getPriceId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbPrice getTbPriceByPriceId(int inPriceId) {
        tbPrice=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Price where PriceId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inPriceId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbPrice == null)tbPrice = getTbPrice(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbPrice;
    }
    public List<TbPrice> getTbPriceAll() {
        tbPriceList=new ArrayList<TbPrice>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Price";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbPriceList.add(getTbPrice(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbPriceList;
    }
    public List<TbPrice> getTbPriceAll(VskPage vp) {
        tbPriceList=new ArrayList<TbPrice>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Price";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Price", "PriceId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbPriceList.add(getTbPrice(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbPriceList;
    }
    public TbPrice getTbPrice(ResultSet rs) throws Exception{
        TbPrice tbPrice = new TbPrice();
        tbPrice.setPriceId(rs.getInt("PriceId"));
        tbPrice.setPriceYear(rs.getString("PriceYear"));
        tbPrice.setPriceValue(rs.getString("PriceValue"));
        tbPrice.setPriceMemo(rs.getString("PriceMemo"));
        tbPrice.setCreateTime(rs.getString("CreateTime"));
        return tbPrice;
    }
}
